//
//  StartupReset.h
//  sad
//
//  Created by Christopher Kemsley on 1/31/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//


#include "ModuleFactory.hpp"
#include "SystemCDefinitions.hpp"
#include <vector>
#include "PubSub.hpp"


#include "Settings.hpp"
#include "Codec.hpp"
#include "Log.hpp"



struct	StartupResetSettings : public Settings
{
	SCInterval	delay ;
	SCInterval	duration ;
	
	std::string	resetName ;
	
	StartupResetSettings ( std::string const & name )
	:	Settings	(name)
	,	delay		( 0 , kSCIntervalSeconds )
	,	duration	( 1 , kSCIntervalMiliSeconds )
	,	resetName	(  )
		{
		add ( "resetSignal" , resetName ) ;
		add ( "duration" , duration ) ;
		add ( "delay" , delay ) ;
		}
	
	virtual void		validate ()
		{
		if ( 0 == duration.to_seconds() )
			Log::warning("StartupResetSettings") << "Startup reset has a duration of zero\n" ;
		}
} ;




class	StartupReset : public ConfiguredModel<StartupResetSettings>
{
public:
	
	SC_HAS_PROCESS(StartupReset) ;
	
	
	StartupReset	( ModelVariant const & name )
	:	ConfiguredModel<StartupResetSettings>	( name )
	,	mReset			( )
		{
		mReset = mConfig.resetName ;
		
		SC_THREAD ( threadMain ) ;
		}
	
	
	
private:
	SCOutputSignal<SCLogic>		mReset ;
	
	
	void	threadMain ()
		{
		Log::states("StartupReset") << "Waiting to start raise reset" ;
		
		wait ( mConfig.delay ) ;
		
		Log::states("StartupReset") << "Resetting" ;
		
		mReset << kSCLogicTrue ;
		
		Log::states("StartupReset") << "Raised reset. Waiting " << mConfig.duration.to_seconds() ;
		
		wait ( mConfig.duration ) ;
		
		Log::states("StartupReset") << "Reset complete. Sinking signal" ;
		
		mReset << kSCLogicFalse ;
		}
} ;




TypedModuleMaker<StartupReset>	_StartupReset ( "StartupReset" ) ;
